package panda.query.tree;

import panda.query.scan.ProductScanner;
import panda.query.scan.Scanner;
import panda.query.struct.Attribute;
import panda.record.Schema;
import panda.transaction.Transaction;

/**
 * 
 * @author Tian Yuan
 */
public class ProductNode implements QueryTreeNode {
	
	private QueryTreeNode l, r;
	private Schema s = new Schema();
	
	public ProductNode(QueryTreeNode l, QueryTreeNode r, Transaction tx) {
		this.l = l;
		this.r = r;
		s.addAll(l.getSchema());
		s.addAll(r.getSchema());
	}

	public Scanner open() {
		return new ProductScanner(l.open(), r.open());
	}
	
	public int blockAccessed() {
		return l.blockAccessed() + r.blockAccessed() * l.outputSize();
	}

	public int distinctSize(Attribute attr) {
		if (l.getSchema().hasAttribute(attr))
			return l.distinctSize(attr);
		else
			return r.distinctSize(attr);
	}


	public int outputSize() {
		return l.outputSize() * r.outputSize();
	}

	public Schema getSchema() {
		return s;
	}

}
